package com.bob.web.dao;

import com.bob.web.common.constant.Page;
import com.bob.web.entity.system.Admin;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.Date;

/**
 * 名称: AdminDao<br>
 * 描述:<br>
 * 类型: JAVA<br>
 * 最近修改时间:2017/7/19 15:19<br>
 *
 * @author BoYangsh
 * @version [版本号, V1.0]
 * @since 2017/7/19 15:19
 */
//@Repository
public class AdminDao extends BaseMongoDaoSupport {
	/**
	 * 根据用户名查找用户
	 * @param username
	 * @return
	 */
	public Admin findAdminByUsername(String username, String...fields) {
		Query query = Query.query(Criteria.where("username").is(username));
		includes(query, fields);
		return findOne(query, Admin.class);
	}

	public void addAdmin(Admin admin) {
		insert(admin);
	}

	public void findPage(Page<Admin> page) {
		findPage(new Query(), page, Admin.class);
	}

	public void add(Admin user) {
		user.setCreateTime(new Date());
		insert(user);
	}

	public void delete(String id) {
		removeById(id, Admin.class);
	}

	public void findAndModify(Query query, Update update) {
		findAndModify(query, update, Admin.class);
	}

	public Admin findUserById(Long id) {
		Query query = Query.query(Criteria.where("_id").is(id));
		return findOne(query, Admin.class);
	}
}
